package com.monkey.tensquare.base.service.impl;

import cn.hutool.core.util.StrUtil;
import com.monkey.tensquare.base.entity.Label;
import com.monkey.tensquare.base.mapper.LabelMapper;
import com.monkey.tensquare.base.service.LabelService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public List<Label> findAll() {
        return labelMapper.findAll();
    }

    @Override
    public void add(Label label) {
        label.setId(idWorker.nextId() + "");
        labelMapper.save(label);
    }

    @Override
    public List<Label> topList() {
        Label label = new Label();
        label.setRecommend("1");
        Specification<Label> specification = createSpecification(label);
        return labelMapper.findAll(specification);
    }

    @Override
    public Label findById(String labelId) {
        return labelMapper.findById(labelId).get();
    }

    @Override
    public void update(Label label) {
        labelMapper.save(label);
    }

    @Override
    public void delete(String labelId) {
        labelMapper.deleteById(labelId);
    }

    @Override
    public List<Label> search(Label label) {
        Specification<Label> specification = createSpecification(label);
        return labelMapper.findAll(specification);
    }

    @Override
    public List<Label> list() {
        Label label = new Label();
        label.setState("1");
        Specification<Label> specification = createSpecification(label);
        return labelMapper.findAll(specification);
    }

    /**
     * 创建查询条件
     *
     * @param label
     * @return 返回查询条件
     */
    private Specification<Label> createSpecification(Label label) {
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (label != null) {
                    if (StrUtil.isNotBlank(label.getLabelname())) {
                        predicateList.add(criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%"));
                    }
                    if (StrUtil.isNotBlank(label.getState())) {
                        predicateList.add(criteriaBuilder.equal(root.get("state").as(String.class), label.getState()));
                    }
                    if (StrUtil.isNotBlank(label.getRecommend())) {
                        predicateList.add(criteriaBuilder.equal(root.get("count").as(String.class), label.getRecommend()));
                    }
                    if (StrUtil.isNotBlank(label.getState())) {
                        predicateList.add(criteriaBuilder.equal(root.get("recommend").as(String.class), label.getState()));
                    }
                    if (StrUtil.isNotBlank(label.getState())) {
                        predicateList.add(criteriaBuilder.equal(root.get("fans").as(String.class), label.getState()));
                    }
                }
                Predicate[] predicates = new Predicate[predicateList.size()];
                predicates = predicateList.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        };
    }

    @Override
    public PageResult<Label> search(Label label, Integer page, Integer size) {
        Specification<Label> specification = createSpecification(label);
        Page<Label> labelPage = labelMapper.findAll(specification, PageRequest.of(page - 1, size));
        return new PageResult<Label>(labelPage.getTotalElements(), labelPage.getContent());
    }


}
